package com.eCommerce.server.dao.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.hibernate.FetchMode;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataAccessException;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import com.eCommerce.corecontract.criteria.CommandeCriteria;
import com.eCommerce.corecontract.dto.CommandeDTO;
import com.eCommerce.corecontract.dto.UserDTO;
import com.eCommerce.corecontract.exception.EcommerceException;
import com.eCommerce.corecontract.exception.EcommercePersisenceException;
import com.eCommerce.server.corebackend.dao.AbstractCommonHibernateDAO;
import com.eCommerce.server.corebackend.dao.ICommandeDAO;



public class CommandeDAOImpl extends AbstractCommonHibernateDAO implements ICommandeDAO {
	private static Logger log =Logger.getLogger(CommandeDAOImpl.class.getName());

	public void delete(CommandeDTO commandeDTO)
			throws EcommercePersisenceException {
		
		try {
			getHibernateTemplate().delete(commandeDTO);
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		
	}


	public List find(CommandeCriteria commandeCriteria)
			throws EcommerceException {
		// TODO Auto-generated method stub
		
		try {
		
			Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
			List resulList =session.createQuery("from CommandeDTO c where c.dateCommande <=:dateDebut AND c.dateCommande>=:dateFin")
			.setLong("dateDebut", commandeCriteria.getDateCommande())
			.setLong("dateFin", commandeCriteria.getDateCommande())			
			.list();
			
			
			
			return resulList;
		} catch (Exception e) {
			Object[] arg=new Object[1];
			EcommercePersisenceException persistenceException=new EcommercePersisenceException(messagesXmlFileName,"ECOMMERCEDAOCOMPOSANT-004",arg,e.getMessage());
			log.error(persistenceException.getMessage());
			throw persistenceException;
		}
			
		
		
		
	}

	public List findCommandeByClient(UserDTO clientdto)
	throws EcommerceException {
		// TODO Auto-generated method stub
		
		try {
		
			Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
			List resulList =session.createQuery("from CommandeDTO c where c.clientDTO=:client")
			.setEntity("client", clientdto)
			.list();
			
			return resulList;
		} catch (Exception e) {
			Object[] arg=new Object[1];
			EcommercePersisenceException persistenceException=new EcommercePersisenceException(messagesXmlFileName,"ECOMMERCEDAOCOMPOSANT-004",arg,e.getMessage());
			log.error(persistenceException.getMessage());
			throw persistenceException;
		}
			
		
		
		
		}
	public List findAll() throws EcommerceException {
		// TODO Auto-generated method stub
		try {
			DetachedCriteria criteria = DetachedCriteria.forClass(CommandeDTO.class);
			
			List listOfComposantDTOReturn=getHibernateTemplate().findByCriteria(criteria);
			Set<CommandeDTO> listOfCommandeDTO= new HashSet<CommandeDTO>();
			
			return listOfComposantDTOReturn;	
		} catch (DataAccessException e) {
			Object[] arg=new Object[1];
			EcommercePersisenceException persistenceException=new EcommercePersisenceException(messagesXmlFileName,"ECOMMERCEDAOCOMPOSANT-004",arg,e.getMessage());
			log.error(persistenceException.getMessage());
			throw persistenceException;
		}
		
	}


	public CommandeDTO findOne(CommandeDTO commandeDTO)	throws EcommerceException {

		try {
			DetachedCriteria criteria = DetachedCriteria.forClass(CommandeDTO.class);
			criteria.add(Restrictions.eq("id", commandeDTO.getId()));
			criteria.setFetchMode("LIGNECOMMANDES", FetchMode.JOIN);
			criteria.setFetchMode("USERS", FetchMode.JOIN);
			List listOfCommande=this.getHibernateTemplate().findByCriteria(criteria);
			commandeDTO = (CommandeDTO) listOfCommande.get(0);

		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DataAccessException e) {
			Object[] arg=new Object[1];
			EcommercePersisenceException persistenceException=new EcommercePersisenceException(messagesXmlFileName,"ECOMMERCEDAOCOMPOSANT-003",arg,e.getMessage());
			log.error(persistenceException.getMessage());
			throw persistenceException;
		}
		return commandeDTO;
	}


	public Long insert(CommandeDTO commandeDTO)
			throws EcommerceException {
		// TODO Auto-generated method stub
		try {
			/* if (!TransactionSynchronizationManager.isSynchronizationActive())
				   TransactionSynchronizationManager.initSynchronization();*/
			this.getHibernateTemplate().getSessionFactory().getCurrentSession().save(commandeDTO);

			return commandeDTO.getId();
		} catch (DataAccessException e) {
			Object[] arg=new Object[1];
			EcommercePersisenceException persistenceException=new EcommercePersisenceException(messagesXmlFileName,"ECOMMERCEDAOCOMPOSANT-001",arg,e.getMessage());
			log.error(persistenceException.getMessage());
			throw persistenceException;
		}
	}


	public void update(CommandeDTO commandeDTO)	throws EcommerceException {
		// TODO Auto-generated method stub
		try {
			this.getHibernateTemplate().update(commandeDTO);
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			Object[] arg=new Object[1];
			EcommercePersisenceException persistenceException=new EcommercePersisenceException(messagesXmlFileName,"ECOMMERCEDAOCOMPOSANT-002",arg,e.getMessage());
			log.error(persistenceException.getMessage());
			throw persistenceException;
		}
		
	}

}
